package com.th.supcom.pds.deptdispensary.client.widget;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.th.supcom.framework.web.gwt.client.widget.FcMessageBox;
import com.th.supcom.pds.common.client.model.CodeConstants;

/**
 * 药品材质药房配置事件中心
 * 
 * @author 杨冲
 */
public class PhamMeterialTypeListener
{
    private DeptVsDispensaryButtons m_buttons;
    private DeptVsDispensaryFields m_fields;
    private PhamMeterialTypeAction m_action;
    private PhamMeterialTypeEditListener m_editListener;

    /**
     * 构造函数
     */
    public PhamMeterialTypeListener ()
    {

    }

    /**
     * 设置按钮组件
     * 
     * @param buttons
     */
    public void setButtons (DeptVsDispensaryButtons buttons)
    {

        this.m_buttons = buttons;
    }

    /**
     * 设置文本组件
     * 
     * @param fields
     */
    public void setFields (DeptVsDispensaryFields fields)
    {

        this.m_fields = fields;
    }

    /**
     * 设置Action
     * 
     * @param action
     */
    public void setAction (PhamMeterialTypeAction action)
    {

        m_action = action;
    }

    /**
     * 初始化事件
     */
    public void initListeners ()
    {
        m_editListener = new PhamMeterialTypeEditListener ();
        m_editListener.setAction (m_action);
        m_editListener.setDeptVsDispensaryButtons (m_buttons);
        m_editListener.initListeners ();

        addAddListener ();
        addEditListener ();
        addRefreshListener ();
        addDeleteListener ();
    }

    /**
     * 新增按钮事件
     */
    private void addAddListener ()
    {
        m_buttons.getAddButton ().addSelectionListener (new SelectionListener <ButtonEvent> ()
        {

            @Override
            public void componentSelected (ButtonEvent ce)
            {
                showWindow (false);
            }
        });
    }

    /**
     * 编辑按钮事件监听
     */
    private void addEditListener ()
    {
        m_buttons.getEditButton ().addSelectionListener (new SelectionListener <ButtonEvent> ()
        {

            @Override
            public void componentSelected (ButtonEvent ce)
            {

                if (!m_action.getGridSelectedStatus ())
                {
                    MessageBox.alert (CodeConstants.PROMPT, "没有选中编辑内容", null);
                    return;
                }

                showWindow (true);
            }

        });
    }

    /**
     * 新增删除按钮事件监听
     */
    private void addDeleteListener ()
    {

        m_buttons.getDeleteButton ().addSelectionListener (new SelectionListener <ButtonEvent> ()
        {

            @Override
            public void componentSelected (ButtonEvent ce)
            {

                if (!m_action.getGridSelectedStatus ())
                {
                    MessageBox.alert (CodeConstants.PROMPT, "没有选中删除内容", null);
                    return;
                }

                FcMessageBox.confirm (CodeConstants.PROMPT, "确认删除？", new Listener <MessageBoxEvent> ()
                {

                    @Override
                    public void handleEvent (MessageBoxEvent be)
                    {

                        if (be.getButtonClicked ().getText ().equals ("否"))
                        {
                            return;
                        }

                        m_action.delete ();
                    }
                });

            }

        });
    }

    /**
     * 刷新事件监听
     */
    private void addRefreshListener ()
    {
        m_buttons.getRefreshButton ().addSelectionListener (new SelectionListener <ButtonEvent> ()
        {

            @Override
            public void componentSelected (ButtonEvent ce)
            {
                m_action.refresh ();
            }

        });
    }

    /**
     * 弹出新增/编辑窗口，true为编辑,false为新增
     * 
     * @param isEdit
     */
    private void showWindow (boolean isEdit)
    {
        PhamMeterialTypeEditWindow window = new PhamMeterialTypeEditWindow (isEdit);
        window.setFields (m_fields);
        window.setButtons (m_buttons);
        window.initUI ();

        m_action.setWindow (window);

        if (isEdit)
        {
            m_action.initDataWhenEdit ();
        }
        else
        {
            m_action.clearEditWindow ();
        }

        m_editListener.setWindow (window);
        m_editListener.setEdit (isEdit);
        window.show ();
    }
}
